//
//  WSQGradientView.m
//  AFNetworking
//
//  Created by 翁胜琼 on 2019/7/20.
//

#import "WSQGradientView.h"

//#define UICOLORFROMHEX(hex) UICOLORFROMHEXA(hex,1)
//#define UICOLORFROMHEXA(hex,a) [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16)) / 255.0 green:((float)((hex & 0xFF00) >> 8)) / 255.0 blue:((float)(hex & 0xFF)) / 255.0 alpha:a]

@implementation WSQGradientView

@synthesize colors = _colors,locations = _locations,endPoint = _endPoint , startPoint = _startPoint;

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setupGradientLayer];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setupGradientLayer];
}

+ (Class)layerClass
{
    return [CAGradientLayer class];
}

- (void)setupGradientLayer
{
    CAGradientLayer *layer = [self getGradientLayer];
    layer.colors = self.colors;
    layer.startPoint = self.startPoint;
    layer.endPoint = self.endPoint;
    layer.locations = self.locations;
}

// MARK: - setter


- (void)setColors:(NSArray *)colors
{
    _colors = [colors copy];
    [self getGradientLayer].colors = colors;
}

- (NSArray *)colors
{
    if (!_colors)
    {
        _colors = @[(__bridge id)[UIColor colorWithRed:249/255.0 green:209/255.0 blue:94/255.0 alpha:1.0].CGColor,(__bridge id)[UIColor colorWithRed:238/255.0 green:167/255.0 blue:82/255.0 alpha:1.0].CGColor];
    }
    return _colors;
}

- (void)setLocations:(NSArray<NSNumber *> *)locations
{
    _locations = [locations copy];
    [self getGradientLayer].locations = locations;
}

- (NSArray<NSNumber *> *)locations
{
    if (!_locations)
    {
        _locations = @[@(0.25),@(0.75)];
    }
    return _locations;
}

- (void)setEndPoint:(CGPoint)endPoint
{
    _endPoint = endPoint;
    [self getGradientLayer].endPoint = endPoint;
}

- (CGPoint)endPoint
{
    if (CGPointEqualToPoint(_endPoint, CGPointZero))
    {
        _endPoint = CGPointMake(1, 0);
    }
    return _endPoint;
}

- (void)setStartPoint:(CGPoint)startPoint
{
    _startPoint = startPoint;
    [self getGradientLayer].startPoint = startPoint;
}

- (CGPoint)startPoint
{
    if (CGPointEqualToPoint(_startPoint, CGPointZero))
    {
        _startPoint = CGPointMake(0, 1);
    }
    return _startPoint;
}

// MARK: - Private Method
- (CAGradientLayer *)getGradientLayer
{
    return (CAGradientLayer *)self.layer;
}

@end
